//
//  CZEmoticonTools.swift
//  WHWeiBo01
//
//  Created by whitcast on 16/7/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//  一页显示20个表情数据
let numbersOfPage = 20
//  读取表情工具专用类
class CZEmoticonTools: NSObject {
    //  定义单例全局访问点
    static let shareTools = CZEmoticonTools()
    
    //  构造函数私有化
    private override init() {
        super.init()

       
    }
    
    //  创建bundle对象
    private lazy var emoticonBundle: NSBundle = {
        
        //  获取路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
        //  创建bundle对象
        let bundle = NSBundle(path: path)!
        
        return bundle
        
    }()
    
    //  读取默认表情数据
    private lazy var defaultEmoticonArray: [CZEmoticon] = {
        
        return self.loadEmoticonsWithPath("default/info.plist")
    
    }()
    //  读取emoji表情数据
    private lazy var emojiEmoticonArray: [CZEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    //  读取浪小花表情数据
    private lazy var lxhEmoticonArray: [CZEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    //  准备表情视图需要的数据
    lazy var allEmoticonArray: [[[CZEmoticon]]] = {
        
       return [
        
                self.pagesWithEmotions(self.defaultEmoticonArray),
                self.pagesWithEmotions(self.emojiEmoticonArray),
                self.pagesWithEmotions(self.lxhEmoticonArray)
        
              ]    
    }()
    
    //  读取表情通用方法,根据子路径
    private func loadEmoticonsWithPath(subPath: String) -> [CZEmoticon]{
        //  获取bundle对象
        let bundle: NSBundle = self.emoticonBundle
        //  获取info.plist文件路径
        let path = bundle.pathForResource(subPath, ofType: nil)!
        //print(path)
        //  获取plist文件里面内容
        let emoticonDicArray = NSArray(contentsOfFile: path)!
        
        var tempArray = [CZEmoticon]()
        //  遍历数组转模型
        for emoticonDic in emoticonDicArray {
            let emoticon = CZEmoticon(dic: emoticonDic as! [String : AnyObject])
            //  如果是图片需要处理路径
            if emoticon.type == "0" {
                let lastPath = (path as NSString).stringByDeletingLastPathComponent
                emoticon.path = lastPath + "/" + emoticon.png!
            }
            
            
            tempArray.append(emoticon)
        }
        
        return tempArray
    
    }
    
    
    //  把表情数据拆分成一个二维数组
    private func pagesWithEmotions(emoticon: [CZEmoticon]) -> [[CZEmoticon]] {
        
        //  根据图片的个数计算页数
        let pages = (emoticon.count - 1) / numbersOfPage + 1
        
        var tempArray =  [[CZEmoticon]]()
        for i in 0..<pages {
            // 截取的索引
            let loc = i * numbersOfPage
            // 截取的个数
            var len = numbersOfPage
            //  如果截取长度不够则取到剩余个数
            if loc + len > emoticon.count {
                len = emoticon.count - loc
            }
            
            let subArray = (emoticon as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [CZEmoticon]
            tempArray.append(subArray)
        }
        
        return tempArray
    
    }
    
    //  根据表情描述字符串查找表情
    func selectEmoticonWithChs(chs: String) -> CZEmoticon? {
        
        //  在默认表情和浪小花表情去查找,因为这个两组数据才是图片
        //  $0 表示闭包中第几个参数 ,  $1...., 
        //  闭包中只有一行代码还可以省略return
        if let defaultEmoticon = defaultEmoticonArray.filter({$0.chs! == chs}).first {
            return defaultEmoticon
        }
        if let lxhEmoticon = lxhEmoticonArray.filter({$0.chs! == chs}).first {
            return lxhEmoticon
        }
        
        
        
        
//        let defaultEmoticon = defaultEmoticonArray.filter { (emoticon) -> Bool in
//            return emoticon.chs! == chs
//        }.first
//        
//        let lxhEmoticon = lxhEmoticonArray.filter { (emoticon) -> Bool in
//            return emoticon.chs! == chs
//        }.first
//        
//        if defaultEmoticon != nil {
//            return defaultEmoticon
//        }
//        if lxhEmoticon != nil {
//            return lxhEmoticon
//        }
        
        
        
    
//        for value in defaultEmoticonArray {
//        
//            if value.chs! == chs {
//                return value
//            }
//        }
//        
//        for value in lxhEmoticonArray {
//            if value.chs! == chs {
//                return value
//            }
//        }
        
        return nil
        
        
        
        
    }
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
